package com.ruoyi.api.controller;

import com.ruoyi.api.annotation.Login;
import com.ruoyi.business.app.bo.*;
import com.ruoyi.business.app.service.IAppUserService;
import com.ruoyi.business.app.vo.AppThirdLoginVo;
import com.ruoyi.business.app.vo.AppUserVo;
import com.ruoyi.business.app.vo.DownloadVo;
import com.ruoyi.business.app.vo.UserAutonymVo;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "用户与账号")
@RestController
@RequestMapping("/api")
@AllArgsConstructor
public class AppUserController extends BaseController {

    private final IAppUserService userService;

    @ApiOperation("注册")
    @PostMapping("/user/register")
    @RepeatSubmit
    public AjaxResult<List<DownloadVo>> userRegister(@RequestBody HcxAppUserBo bo) {
        return AjaxResult.success(userService.userRegister(bo));
    }

    @ApiOperation("获取手机验证码")
    @GetMapping("/user/authcode")
    @RepeatSubmit
    public AjaxResult authCode(@ApiParam(value = "手机号", required = true) String phonenumber, HttpServletRequest request) throws Exception {
        return userService.sendCode(phonenumber, request);
    }

    @ApiOperation("密码验证登录")
    @PostMapping("/user/pwd/login")
    @RepeatSubmit
    public AjaxResult pwdLogin(@ApiParam(value = "手机号", required = true) @RequestParam @NotBlank String phonenumber,
                               @ApiParam(value = "密码", required = true) @RequestParam @NotBlank String password) {
        return AjaxResult.success("登录成功", userService.pwdLogin(phonenumber, password));
    }

    @ApiOperation("短信验证码登录")
    @PostMapping("/user/authcode/login")
    @RepeatSubmit
    public AjaxResult authCodeLogin(@ApiParam(value = "手机号", required = true) @RequestParam @NotBlank String phonenumber,
                                    @ApiParam(value = "短信验证码", required = true) @RequestParam @NotBlank String authCode) {
        return AjaxResult.success("获取验证码成功", userService.authcodeLogin(phonenumber, authCode));
    }

    @Login
    @ApiOperation("实名认证")
    @PostMapping("/user/autonym")
    @RepeatSubmit
    public AjaxResult userRegister(@RequestBody UserAutonymBo bo) {
        return toAjax(userService.userAutonym(bo));
    }

    @Login
    @ApiOperation("实名信息查询")
    @GetMapping("/user/getAutonym")
    public AjaxResult<UserAutonymVo> getAutonym() {
        return AjaxResult.success(userService.getAutonym());
    }


    @ApiOperation("第三方登录")
    @PostMapping("/user/third")
    @RepeatSubmit
    public AjaxResult<AppThirdLoginVo> thirdLogin(@RequestBody ThirdLoginBo bo) {
        return userService.thirdLogin(bo);
    }


    @ApiOperation("绑定手机号")
    @PostMapping("/user/savePhone")
    @RepeatSubmit
    public AjaxResult<String> savePhoneNumber(@RequestBody UserSavePhone bo) {
        return AjaxResult.success("绑定手机号成功", userService.savePhoneNumber(bo));
    }

    @Login
    @ApiOperation("个人资料")
    @GetMapping("/user/personalDate")
    public AjaxResult<AppUserVo> getPersonalDate() {
        return AjaxResult.success(userService.getPersonalDate());
    }

    @Login
    @ApiOperation("修改密码-旧密码")
    @PostMapping("/user/amend/password")
    @RepeatSubmit
    public AjaxResult amendPassword(@RequestBody UserPwdBo bo) {
        return toAjax(userService.amendPassword(bo));
    }

    @Login
    @ApiOperation("旧密码校验")
    @PostMapping("/user/pwdVerify")
    @RepeatSubmit
    public AjaxResult pwdVerify(@ApiParam(value = "旧密码", required = true) @RequestParam @NotBlank String oldPassword) {
        return toAjax(userService.pwdVerify(oldPassword));
    }

    @Login
    @ApiOperation("修改支付密码")
    @PostMapping("/user/amend/payCode")
    @RepeatSubmit
    public AjaxResult amendPayCode(@RequestBody PayPwdBo bo) {
        return toAjax(userService.amendPayCode(bo));
    }


    @Login
    @ApiOperation("手机号修改支付密码")
    @PostMapping("/user/phone/payCode")
    @RepeatSubmit
    public AjaxResult phoneCode(@RequestBody PayPwdBo bo) {
        return toAjax(userService.phoneCode(bo));
    }


    @Login
    @ApiOperation("旧支付密码校验")
    @PostMapping("/user/payCodeVerify")
    @RepeatSubmit
    public AjaxResult payCodeVerify(@ApiParam(value = "旧密码", required = true) @RequestParam @NotBlank String oldPayCode) {
        return toAjax(userService.payCodeVerify(oldPayCode));
    }


    @Login
    @ApiOperation("校验验证码")
    @PostMapping("/user/authCodeVerify")
    @RepeatSubmit
    public AjaxResult authCodeVerify(@ApiParam(value = "手机号", required = true) @RequestParam @NotBlank String phonenumber,
                                     @ApiParam(value = "短信验证码", required = true) @RequestParam @NotBlank String authCode) {
        return toAjax(userService.authCode(phonenumber, authCode));
    }

    @Login
    @ApiOperation("意见反馈")
    @PostMapping("/user/feedback")
    @RepeatSubmit
    public AjaxResult feedback(@RequestBody HcxUserOpinionsBo bo) {
        return toAjax(userService.feedback(bo));
    }

    @Login
    @ApiOperation("更改绑定手机号")
    @PostMapping("/user/changePhonenumber")
    @RepeatSubmit
    public AjaxResult changePhonenumber(@ApiParam(value = "手机号", required = true) @RequestParam @NotBlank String phonenumber,
                                        @ApiParam(value = "短信验证码", required = true) @RequestParam @NotBlank String authCode) {
        return toAjax(userService.changePhonenumber(phonenumber, authCode));
    }

    @Login
    @ApiOperation("修改个人资料")
    @PostMapping("/user/update")
    @RepeatSubmit
    public AjaxResult update(@RequestBody AppUserUpdateBo bo) {
        return toAjax(userService.update(bo));
    }


    @ApiOperation("忘记密码")
    @PostMapping("/user/resetpwd")
    @RepeatSubmit
    public AjaxResult resetpwd(@RequestBody AppResetpwd bo) {
        return toAjax(userService.resetpwd(bo));
    }

    @Login
    @ApiOperation("删除用户接口")
    @PostMapping("/deleteAppUser")
    @Transactional
    public AjaxResult deleteAppUser() {
        // 删除用户
        boolean b = userService.removeById(SecurityUtils.getUserId());
        if (b) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
